﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;

using QuickGraph;
using QuickGraph.Serialization;

using BOL.IO;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public class GraphMLReader : IReader<Network>
    {
        #region Private variables

        private FileStream _fs;

        #endregion

        #region Constructors

        public GraphMLReader(FileStream fs)
        {
            _fs = fs;
        }

        public GraphMLReader(string path) : this(new FileStream(path, FileMode.Open)) { }

        #endregion

        #region Public methods

        public Network Read()
        {
            using(var xr = XmlReader.Create(_fs))
            {
                var network = new Network();

                network.DeserializeFromGraphML<Gene, Relation, Network>(
                    xr,
                    id => new Gene(Int32.Parse(id)),
                    (source, target, id) => new Relation(source, target, UInt32.Parse(id))
                );

                return network;
            }
        }

        public void Close()
        {
            _fs.Close();
        }

        #endregion
    }
}
